﻿#region Copyright © 2010 Chad Albrecht & Mark Adams
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

/* © BLOKUS and associated trademarks are copyright Mattel, Inc.   */
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Threading;

namespace Blokus
{
    public partial class MainPage : UserControl
    {
        private GameBoard _gb = null;
        private TileWell _tw1 = null;
        private TileWell _tw2 = null;
        private GameController _gc = null;

        public MainPage()
        {
            InitializeComponent();

            this.board.Visibility = Visibility.Visible;                

            this.Loaded += this.OnRootControlLoaded;
            this.KeyDown += new KeyEventHandler(MainPage_KeyDown);                        
        }

        void ScrollViewer_KeyEvent(object sender, KeyEventArgs e)
        {
            //System.Diagnostics.Debug.WriteLine(String.Format("ScrollViewer {0} key pressed.", e.Key));            
            MainPage_KeyDown(sender, e);
        }

        void MainPage_KeyDown(object sender, KeyEventArgs e)
        {            
            //System.Diagnostics.Debug.WriteLine(String.Format("{0} key pressed.", e.Key));
            if ((this._tw1 != null) && (this._tw1.PieceBeingDragged != null))
                this._tw1.KeyPress(e);
            else if ((this._tw2 != null) && (this._tw2.PieceBeingDragged != null))
                this._tw2.KeyPress(e);

            e.Handled = true;
        }       

        private void OnRootControlLoaded(object sender, RoutedEventArgs e)
        {
            // Wire up the TileWells to the Gameboard
            this._gb = (GameBoard)this.FindName("gameBoard1");
            this._tw1 = (TileWell)this.FindName("tileWell1");
            this._tw2 = (TileWell)this.FindName("tileWell2");

            this._tw1.TileSize = this._gb.Width / GameBoard.ROWS;
            this._tw2.TileSize = this._gb.Width / GameBoard.ROWS;
            
            this._tw1.gameBoard = this._gb;
            this._tw2.gameBoard = this._gb;

            this._gc = new GameController(this._gb, this._tw1, this._tw2);
            this._gc.GameOver += new GameController.GameOverHandler(_gc_GameOver);
        }

        void _gc_GameOver(object sender)
        {
            GameController gc = (GameController)sender;

            WinScreen s = new WinScreen(gc.getPlayer1Score(), gc.getPlayer2Score());
            s.Closed += new System.EventHandler(GameOverDialog_Closed);
            s.Show();
        }


        private void GameOverDialog_Closed(object sender, EventArgs e)
        {
            this._gc.RestartGame();
        }

        private void RestartButton_Click(object sender, RoutedEventArgs e)
        {
            RestartDialog dialog = new RestartDialog();
            dialog.Closed +=  new System.EventHandler(RestartDialog_Closed);
            dialog.Show();

        }

        private void RestartDialog_Closed(object sender, EventArgs e)
        {
            RestartDialog dialog = (RestartDialog)sender;
            
            if (dialog.DialogResult.Value)
            {
                this._gc.RestartGame();
            }
        }

        private void HelpButton_Click(object sender, RoutedEventArgs e)
        {   
            HelpDialog hWin = new HelpDialog();
            hWin.Show();

        }
                     
       
    }
    
}
